package com.softeem.musicserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.musicserver.dto.R;
import com.softeem.musicserver.entity.Listdetails;
import com.softeem.musicserver.service.ListdetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Tag(name = "歌单详情管理",description = "歌单和歌曲关联信息管理，歌单详情添加，删除，查询")
@CrossOrigin
@RestController
public class ListdetailsController {

    @Autowired
    private ListdetailsService listdetailsService;

    @Operation(summary = "添加歌单详情",description = "将歌单和歌曲关联")
    @Parameters({
            @Parameter(name = "listId",description = "歌单id",required = true),
            @Parameter(name = "mid",description = "歌曲id" ,required = true)
    })
    @PostMapping("/listdetails/add")
    public R addListdetails(@RequestParam Integer listId, @RequestParam Integer mid){
        QueryWrapper<Listdetails> query = Wrappers.query(Listdetails.class);
        //select * from listdetails where listid=? & mid=?
        query.eq("listid",listId)
                .eq("mid",mid);
        Listdetails one = listdetailsService.getOne(query);
        if(Objects.nonNull(one)){
            return R.fail().setMsg("该歌曲已经在歌单中存在");
        }
        Listdetails details = new Listdetails();
        details.setListid(listId);
        details.setMid(mid);
        if(listdetailsService.save(details)){
            return R.ok().setMsg("成功加入歌单");
        }
        return R.fail().setMsg("加入歌单失败");
    }
}
